package com.seedog.mq.model;

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

/**
 * 消息模型类，代表在消息队列中传递的消息
 */
public class Message implements Serializable {
    private static final long serialVersionUID = 1L;
    
    // 消息唯一标识
    private String messageId;
    // 消息主题
    private String topic;
    // 消息内容
    private byte[] body;
    // 消息创建时间
    private Date timestamp;
    // 消息过期时间
    private long expireTime;
    // 消息属性
    private MessageProperties properties;
    
    public Message() {
        this.messageId = UUID.randomUUID().toString();
        this.timestamp = new Date();
        this.properties = new MessageProperties();
    }
    
    public Message(String topic, byte[] body) {
        this();
        this.topic = topic;
        this.body = body;
    }
    
    public Message(String topic, String content) {
        this();
        this.topic = topic;
        this.body = content.getBytes();
    }
    
    // Getters and Setters
    public String getMessageId() {
        return messageId;
    }
    
    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }
    
    public String getTopic() {
        return topic;
    }
    
    public void setTopic(String topic) {
        this.topic = topic;
    }
    
    public byte[] getBody() {
        return body;
    }
    
    public void setBody(byte[] body) {
        this.body = body;
    }
    
    public Date getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }
    
    public long getExpireTime() {
        return expireTime;
    }
    
    public void setExpireTime(long expireTime) {
        this.expireTime = expireTime;
    }
    
    public MessageProperties getProperties() {
        return properties;
    }
    
    public void setProperties(MessageProperties properties) {
        this.properties = properties;
    }
    
    // 检查消息是否已过期
    public boolean isExpired() {
        if (expireTime <= 0) {
            return false;
        }
        return System.currentTimeMillis() > (timestamp.getTime() + expireTime);
    }
    
    @Override
    public String toString() {
        return "Message{" +
                "messageId='" + messageId + '\'' +
                ", topic='" + topic + '\'' +
                ", timestamp=" + timestamp +
                ", expireTime=" + expireTime +
                '}';
    }
}